<?php
class CPyramidMemberShare extends Zend_Db_Table
{
	protected $_name = '';
	protected $_primary = '';
	
	public function __construct()
	{
		$this->_name = R_PRE . 'pyramid_member_share';
		$this->_primary = 'member_share_id';
		parent::__construct();
	}
	
	public function add($row)
	{
		return $this->insert($row);
	}
	
	public function getFriendsShareCount($facebookId)
	{
		$db = $this->getAdapter();
		$where = $db->quoteInto('from_facebook_id = ?', $facebookId);
		
		$sql = 'select count(*) as c from ' . $this->_name . ' where ' . $where;
		$result = $db->fetchAll($sql);
		return $result[0]['c'];
	}
	
	public function getMyShareCount($facebookId)
	{
		$db = $this->getAdapter();
		$where = $db->quoteInto('facebook_id = ?', $facebookId);
		
		$sql = 'select count(*) as c from ' . $this->_name . ' where ' . $where;
		$result = $db->fetchAll($sql);
		return $result[0]['c'];
	}
	
	public function getAll($page, $pageNum, $facebookId = 0)
	{
		$offset = ($page - 1) * $pageNum;
		$where = array();
		if ($facebookId) {
			$where['facebook_id = ?'] = $facebookId;
		}
		
		return $this->fetchAll($where, 'add_time desc', $pageNum, $offset);
	}
	
	public function getPageCount($pageNum, $facebookId = 0)
	{
		$where = array();
		if ($facebookId) {
			$where['facebook_id = ?'] = $facebookId;
		}
		
		$rows = $this->fetchAll($where);
		return count($rows);
	}
	
	public function getAllVisit($page, $pageNum)
	{
		$offset = ($page - 1) * $pageNum;
		$sql = 'select a.* from ' . $this->_name . ' as a group by a.facebook_id order by a.add_time desc limit ' . $offset . ',' . $pageNum;
		
		return $this->getAdapter()->fetchAll($sql);
	}
	
	public function getPageCountVisit($pageNum)
	{
		$sql = 'select a.* from ' . $this->_name . ' as a group by a.facebook_id';
		$rows = $this->getAdapter()->fetchAll($sql);
		return count($rows);
	}
	
	public function getShareCount()
	{
		$sql = 'select count(distinct(facebook_id)) as c from ' . $this->_name;
		$db = $this->getAdapter();
		$result = $db->fetchAll($sql);
		return $result[0]['c'];
	}
	
	public function getToShareCount()
	{
		$sql = 'select count(distinct(to_facebook_id)) as c from ' . $this->_name;
		$db = $this->getAdapter();
		$result = $db->fetchAll($sql);
		return $result[0]['c'];
	}
}